package com.teamwings.ctrls.formCtrls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.page.service.IPageService;

import java.util.List;
import java.util.Map;

@FormControl(type = "oa_dept")
public class Dept extends FormComponent {
    public Dept() {
        this.type = "oa_dept";
        this.fieldDefaultLength = "100";
        this.fieldType = "VARCHAR";
    }

    @Override
    public Result<?> trigger(FormConstant.TRIGGER_TYPE type, IPageService pageService,
                             FormVo formDesignCode, Object paramsData) {
        JSONObject jo = JSON.parseObject(paramsData + "");
        Integer listType = jo.getInteger("listType");
        // 查询部门
        // 1. 查询所有部门
        // 2. 查询用户所在的部门(未实现)
        // 3. 查询控件给出的“检查条件语句”进行部门查询(未实现)
        if (type == FormConstant.TRIGGER_TYPE.CTRL_HANDLER) {
            String sql = "";
            // 查询所有部门
            if (listType.equals(0)) {
                return listAll(jo, pageService);
            } else if(listType.equals(1)) {
                //
            } else if(listType.equals(2)){
                //
            }
        }
        return Result.error("参数有误，查询失败。");
    }

    /**
     * 查询所有部门
     * @param jo
     * @param pageService
     */
    private Result<?> listAll(JSONObject jo, IPageService pageService) {
        String sql = "SELECT depart_name, id, parent_id FROM sys_depart WHERE del_flag = 0 ORDER BY depart_order ASC";
        List<Map> maps = pageService.executeSql(sql);
        return Result.OK("查询成功", maps);
    }
}
